<?php
// @formatter:off
/**
 * @file WCUserData.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-18 22:13:05 +0000 (Thu, 18 Jul 2013) $
 *
 * $Id: WCUserData.php 97 2013-07-18 22:13:05Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/includes/representations/WCUserData.php $
 */
// @formatter:on

/**
 * @class WCUserData This class holds the logic to manage user's data beyond
 * username and password.
 * User data is separated into two different types:
 *	- Basic Information: This are column of table '%user_data'. In this case:
 *		+ e-mail
 *		+ Full Name
 *		+ Avatar
 *	- Dynamic information: This type of information has a variable amount of
 *	  fields depending on '$wcUserXData["_names"]'. All this fields and their
 *	  values are manages as an associative array serialized and stored in a
 *	  column named 'usd_extra'.
 */
class WCUserData extends WCTableRepresentation {
	/**
	 * @var string[] Check WCTableRepresentation class documentation.
	 */
	protected $_PropertiesReadOnly = array("id");
	/**
	 * @var string Check WCTableRepresentation class documentation.
	 */
	protected $_PropertiesPrefix = "usd_";
	/**
	 * @var string Check WCTableRepresentation class documentation.
	 */
	protected $_PropertiesTable = "user_data";
	/**
	 * @var string Check WCTableRepresentation class documentation.
	 */
	protected $_PropertiesNameColumn = "fullname";
	/**
	 * @var string[] Check WCTableRepresentation class documentation.
	 */
	protected $_PropertiesNULL = array(
		"email",
		"extra",
		"fullname"
	);
	/**
	 * @var string[string] Check WCTableRepresentation class documentation.
	 */
	protected $_PropertiesPresetFunc = array("extra" => "serialize");
	//
	// Public methods.
	/**
	 * This method returns a path for user's avatar relative to $wcROOTURI
	 * and based on it's extra information configuration.
	 *
	 * @return string Returns an image URI for a stored avatar image.
	 */
	public function avatar() {
		//
		// Setting a default value to be returned.
		$out = "";
		//
		// Loading global pointers.
		global $wcDefaults;
		//
		// Checking if current user is root or not.
		if(in_array($this->id(), array(
			WC_USER_ROOT_ID,
			WC_USER_CRON_ID
		))) {
			//
			// Root user always have the same avatar and it can't be
			// changed.
			$out = "{$wcDefaults["avatar-basic"]}/root.png";
		} else {
			//
			// Expanding avatar configuration.
			$avatar = explode("|", $this->avatar);
			//
			// Checking avatar type.
			switch(strtoupper($avatar[0])) {
				case "B":
					//
					// Basic: This avatar's kind is stored in
					// a general directory that belongs to
					// 'wcomix' itself.
					$out = "{$wcDefaults["avatar-basic"]}/{$avatar[1]}";
					break;
				case "T":
					//
					// By theme: This avatar's kind is stored
					// into a particular directory provided
					// current theme.
					$out = "{$wcDefaults["avatar-theme"]}/{$avatar[1]}";
					break;
				case "U":
				case "C":
					//
					// By user: This avatar's kind is stored
					// into a special directory inside
					// 'wcomix' cache. These avatars may have
					// been uploaded by the user or not.
					$out = "{$wcDefaults["avatar-cache"]}/{$this->id()}{$avatar[1]}";
					break;
				default:
					//
					// At this point, current user has no
					// avatar assigned.
					$out = $wcDefaults["preview"];
					break;
			}
		}
		//
		// Returning avatars URI.
		return $out;
	}
	/**
	 * This method allows to access user's full name, the one that may not be
	 * its username.
	 *
	 * @return string Returns user's full name.
	 */
	public function fullname() {
		return $this->fullname;
	}
	/**
	 * This method allows to access a dynamic field of an user. If it's not
	 * present, it enforces it existence.
	 *
	 * @param string $name Name of the property to look for.
	 * @return mixed Returns the value of the requested property.
	 */
	public function getExtra($name) {
		//
		// Loading global pointers.
		global $wcUserXData;
		//
		// Setting a default value to be returned.
		$out = false;
		//
		// Creating a shortcut for current the extra information.
		$extraInfo = $this->extra;
		//
		// The requested property must be a valid one.
		if(!in_array($name, $wcUserXData["_names"])) {
			trigger_error("'{$name}' is not defined as a valid extra user datum.", E_USER_ERROR);
		}
		//
		// Checking if the requested property is present.
		if(!isset($extraInfo[$name])) {
			//
			// At this point, the property doesn't exists and it must.
			//
			// Seeking the lacking property.
			foreach($wcUserXData as $key => $value) {
				//
				// If the current item is a property specification
				// and it has the name of the lacking property, it
				// is set with a default value.
				if(is_numeric($key) && $value["name"] == $name) {
					$this->setExtra($name, $value["default"]);
					$out = $value["default"];
					break;
				}
			}
		} else {
			//
			// At this point, the property is present and it has a
			// value.
			$out = $extraInfo[$name];
		}
		//
		// Returning dynamic field value.
		return $out;
	}
	/**
	 * This method is just a redefinition of the inherited class needed to
	 * perform some specific tasks.
	 *
	 * @param int $id ID of the item to load.
	 * @return boolean Returns true when the object was successfully
	 * loaded.
	 */
	public function load($id) {
		//
		// Calling the inherited method.
		parent::load($id);
		//
		// Checking the object loaded successfully.
		if($this->ok()) {
			//
			// Decoding dynamic user information.
			$this->_info["usd_extra"] = unserialize($this->_info["usd_extra"]);
		}
		//
		// Returning object status.
		return $this->ok();
	}
	/**
	 * This method allows to modify a dynamic field of an user.
	 *
	 * @param string $name Name of the property to modify.
	 * @param mixed $value Value to set.
	 * @return mixed Returns the value of the modified property.
	 */
	public function setExtra($name, $value) {
		//
		// Loading global pointers.
		global $wcUserXData;
		//
		// Creating a copy for current the extra information.
		$extraInfo = $this->extra;
		//
		// The specified property must be a valid one.
		if(!in_array($name, $wcUserXData["_names"])) {
			trigger_error("'{$name}' is not defined as a valid extra user datum.", E_USER_ERROR);
		}
		//
		// Setting the new value on the copy.
		$extraInfo[$name] = $value;
		//
		// Triggering the real update of the database.
		$this->extra = $extraInfo;
		//
		// Returning dynamic field value.
		return $this->getExtra($name);
	}
}
?>